﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using SD.LLBLGen.Pro.ORMSupportClasses;
using CARMSBusiness;
using CARMSData.CollectionClasses;
using CARMSData.EntityClasses;
using CARMSData.HelperClasses;

namespace CARMS.Admin
{
    [Function("Admin.DataDictionary")]
    public partial class DataDictionary : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowTypes();
            }
        }
        private void ShowTypes()
        {
            BindTypes();
            if (ddlDicType.Items.Count > 0)
            {
                ddlDicType.Items[0].Selected = true;
                btnCreateItem.Visible = true;
                ShowItemsByTypeID();
            }
        }
        private void ShowItemsByTypeID()
        {
            if (ddlDicType.SelectedItem != null)
            {
                var selectedTypeID = int.Parse( ddlDicType.SelectedItem.Value);
                var itemCollection = new DictionaryCollection();
                itemCollection.GetMulti(DictionaryFields.CategoryId == selectedTypeID,0,new SortExpression{DictionaryFields.SortIndex|SortOperator.Ascending});
                gridItem.DataSource = itemCollection;
                gridItem.DataBind();
            }
        }
        protected void ddlDicType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ShowItemsByTypeID();
            btnCreateItem.Visible = true;

        }
        protected void gridItem_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            var row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
            switch (e.CommandName)
            {
                case "DeleteItem":
                    var typeID = int.Parse(e.CommandArgument.ToString());
                    if (AdminBusiness.DeleteDictionary(typeID))
                    {
                        ShowItemsByTypeID();
                        base.Alert("Successfully deleted!");
                    }
                    else
                    {
                        base.Alert("Delete failed!");
                    }
                    break;
            }
        }

        protected void gridItem_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (DictionaryEntity)e.Row.DataItem;
                var btnModify = e.Row.FindControl("btnModify") as ImageButton;
                btnModify.OnClientClick = "OpenEditItemWindow('" + entity.Oid + "');return false;";
            }
        }
        protected void btnRefreshEntries_Click(object sender, EventArgs e)
        {
            ShowTypes();
        }
        private void BindTypes()
        {
            var typeCollection = new DictionaryCategoryCollection();
            typeCollection.GetMulti(DictionaryCategoryFields.IsShow==true, 0, new SortExpression { DictionaryCategoryFields.Name | SortOperator.Ascending });
            ddlDicType.DataTextField = "Name";
            ddlDicType.DataValueField = "OID";
            ddlDicType.DataSource = typeCollection;
            ddlDicType.DataBind();
            if (ddlDicType.Items.Count > 0)
            {
                ddlDicType.Items[0].Selected = true;
                btnCreateItem.Visible = true;
            }
        }
        protected void btnCreateType_Click(object sender, EventArgs e)
        {
            BindTypes();
        }
        protected void btnCreateItem_Click(object sender, EventArgs e)
        {
            base.ScriptStartup(" OpenCreateItemWindow('" + ddlDicType.SelectedValue + "')");
        }

        protected void btnCreateItemHidden_Click(object sender, EventArgs e)
        {
            ShowItemsByTypeID();
        }
    }
}
